PROJECT(application)

CONFIGURE_FILE(config.h.in config.h)
CONFIGURE_FILE(Info.plist.in Info.plist @ONLY)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
INCLUDE_DIRECTORIES(${k3d_SOURCE_DIR})
INCLUDE_DIRECTORIES(${k3dsdk_BINARY_DIR})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${K3D_GLIBMM_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${K3D_SIGC_INCLUDE_DIRS})

LINK_DIRECTORIES(${K3D_SIGC_LIB_DIRS})

SET(SOURCES k3d_main.cpp)

IF(WIN32 AND MSVC)
	SET(SOURCES ${SOURCES} winmain.cpp)
ELSE(WIN32 AND MSVC)
	SET(SOURCES ${SOURCES} main.cpp)
ENDIF(WIN32 AND MSVC)

IF(WIN32)
	IF(MINGW)
		ADD_CUSTOM_COMMAND(
			OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/icon.o
			COMMAND
				windres.exe
				-I${CMAKE_CURRENT_SOURCE_DIR}
				-i${CMAKE_CURRENT_SOURCE_DIR}/icon.rc
				-o ${CMAKE_CURRENT_BINARY_DIR}/icon.o
			)

		SET(SOURCES ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/icon.o)
	ELSE(MINGW)
		SET(SOURCES ${SOURCES} icon.rc)
	ENDIF(MINGW)
ENDIF(WIN32)

ADD_EXECUTABLE(k3d WIN32 ${SOURCES})

SET_TARGET_PROPERTIES(k3d PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY ${K3D_RUNTIME_OUTPUT_DIRECTORY}
  )

TARGET_LINK_LIBRARIES(k3d
	k3dsdk
	k3dsdk-opengl
	)

IF(UNIX AND NOT APPLE)
	SET_TARGET_PROPERTIES(k3d PROPERTIES LINK_FLAGS -Wl,-E)
ENDIF()

IF(K3D_ENABLE_NLS)
	INCLUDE_DIRECTORIES(${K3D_INTL_INCLUDE_DIRS})
	TARGET_LINK_LIBRARIES(k3d ${K3D_INTL_LIBS})
ENDIF(K3D_ENABLE_NLS)

IF(APPLE AND K3D_ENABLE_OSX_BUNDLE)
  SET_TARGET_PROPERTIES(k3d PROPERTIES
    RUNTIME_OUTPUT_NAME K-3D
    )

  ADD_CUSTOM_COMMAND(
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/Info.plist
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/Info.plist ${K3D_BUNDLE_DIRECTORY}
    OUTPUT ${K3D_BUNDLE_DIRECTORY}/Info.plist
    )
  ADD_CUSTOM_COMMAND(
    DEPENDS k3d
    COMMAND ${CMAKE_COMMAND} -E make_directory ${K3D_BUNDLE_DIRECTORY}/Resources
    OUTPUT ${K3D_BUNDLE_DIRECTORY}/Resources
    )
  ADD_CUSTOM_COMMAND(
    DEPENDS ${K3D_BUNDLE_DIRECTORY}/Resources ${share_SOURCE_DIR}/k3d/icons/k3d.icns
    COMMAND ${CMAKE_COMMAND} -E copy ${share_SOURCE_DIR}/k3d/icons/k3d.icns ${K3D_BUNDLE_DIRECTORY}/Resources/K-3D.icns
    OUTPUT ${K3D_BUNDLE_DIRECTORY}/Resources/K-3D.icns
    )
  ADD_CUSTOM_TARGET(bundle ALL
    DEPENDS
      ${K3D_BUNDLE_DIRECTORY}/Info.plist
      ${K3D_BUNDLE_DIRECTORY}/Resources/K-3D.icns
    )
ENDIF()

# Setup convenience targets for running the application out of the build directory ...
FIND_PROGRAM(GDB_COMMAND gdb)
SET(K3D_EXTRA_GDB_ARGUMENTS "" CACHE STRING "Extra arguments to pass to gdb for debugging.")

GET_TARGET_PROPERTY(K3D_COMMAND k3d LOCATION)

SET(K3D_DEFAULT_ARGUMENTS "")
LIST(APPEND K3D_DEFAULT_ARGUMENTS "--color")
LIST(APPEND K3D_DEFAULT_ARGUMENTS "--log-level=debug")
LIST(APPEND K3D_DEFAULT_ARGUMENTS "--add-path=${k3d_BINARY_DIR}/bin")
LIST(APPEND K3D_DEFAULT_ARGUMENTS "--locale=${k3d_BINARY_DIR}/po")

SET(K3D_GDB_COMMAND_LINE ${GDB_COMMAND} ${K3D_EXTRA_GDB_ARGUMENTS})
SET(K3D_NGUI_COMMAND_LINE ${K3D_COMMAND} ${K3D_DEFAULT_ARGUMENTS} --ui=ngui)
SET(K3D_NUI_COMMAND_LINE ${K3D_COMMAND} ${K3D_DEFAULT_ARGUMENTS} --ui=nui)
SET(K3D_PYUI_COMMAND_LINE ${K3D_COMMAND} ${K3D_DEFAULT_ARGUMENTS} --ui=pyui)
SET(K3D_QTUI_COMMAND_LINE ${K3D_COMMAND} ${K3D_DEFAULT_ARGUMENTS} --ui=qtui)

K3D_ADD_CUSTOM_TARGET(run-ngui REQUIRES K3D_BUILD_NGUI_MODULE COMMAND ${K3D_NGUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-ngui-gdb REQUIRES K3D_BUILD_NGUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_NGUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-nui REQUIRES K3D_BUILD_NUI_MODULE COMMAND ${K3D_NUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-nui-gdb REQUIRES K3D_BUILD_NUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_NUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-pyui REQUIRES K3D_BUILD_PYUI_MODULE COMMAND ${K3D_PYUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-pyui-gdb REQUIRES K3D_BUILD_PYUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_PYUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-qtui REQUIRES K3D_BUILD_QTUI_MODULE COMMAND ${K3D_QTUI_COMMAND_LINE})
K3D_ADD_CUSTOM_TARGET(run-qtui-gdb REQUIRES K3D_BUILD_QTUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_QTUI_COMMAND_LINE})

IF(K3D_BUILD_NGUI_MODULE)
	K3D_ADD_CUSTOM_TARGET(run REQUIRES K3D_BUILD_NGUI_MODULE COMMAND ${K3D_NGUI_COMMAND_LINE})
	K3D_ADD_CUSTOM_TARGET(run-gdb REQUIRES K3D_BUILD_NGUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_NGUI_COMMAND_LINE})
ELSEIF(K3D_BUILD_QTUI_MODULE)
	K3D_ADD_CUSTOM_TARGET(run REQUIRES K3D_BUILD_QTUI_MODULE COMMAND ${K3D_QTUI_COMMAND_LINE})
	K3D_ADD_CUSTOM_TARGET(run-gdb REQUIRES K3D_BUILD_QTUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_QTUI_COMMAND_LINE})
ELSEIF(K3D_BUILD_PYUI_MODULE)
	K3D_ADD_CUSTOM_TARGET(run REQUIRES K3D_BUILD_PYUI_MODULE COMMAND ${K3D_PYUI_COMMAND_LINE})
	K3D_ADD_CUSTOM_TARGET(run-gdb REQUIRES K3D_BUILD_PYUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_PYUI_COMMAND_LINE})
ELSEIF(K3D_BUILD_NUI_MODULE)
	K3D_ADD_CUSTOM_TARGET(run REQUIRES K3D_BUILD_NUI_MODULE COMMAND ${K3D_NUI_COMMAND_LINE})
	K3D_ADD_CUSTOM_TARGET(run-gdb REQUIRES K3D_BUILD_NUI_MODULE GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} --args ${K3D_NUI_COMMAND_LINE})
ENDIF()

K3D_ADD_CUSTOM_TARGET(run-gdb-core REQUIRES GDB_COMMAND COMMAND ${K3D_GDB_COMMAND_LINE} ${K3D_COMMAND} core)
#ADD_CUSTOM_TARGET(run-valgrind COMMAND valgrind -v ${k3d_BINARY_DIR}/bin/k3d ${K3D_DEFAULT_ARGUMENTS} --ui=${K3D_NGUI_BINARY})
#ADD_CUSTOM_TARGET(run-strace COMMAND strace ${k3d_BINARY_DIR}/bin/k3d ${K3D_DEFAULT_ARGUMENTS} --ui=${K3D_NGUI_BINARY})

INSTALL(TARGETS k3d DESTINATION bin)

